﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

/*************************************************
 * This program is free software: you can redistribute it 
 * and/or modify it but please leave header.
 * 
 * Created by NeoModulus
 * **********************************************/
namespace QuicksandCRUD
{
    [Serializable]
    public abstract class ResourceBase
    {
        protected ResourceBase()
        {
            ResourceID = Guid.NewGuid();
        }

        [XmlIgnore]
        public virtual string RESOURCELOCATION { get { return @"Base"; } }

        [XmlIgnore]
        public virtual string RESOURCEKEY { get { return @"ResourceBase"; } }

        [XmlIgnore]
        public virtual Type RESOURCETYPE { get { return typeof(ResourceBase); } }

        public Guid ResourceID { get; set; }

        public int ResourceSortID { get; set; }

        public List<ResourceBase> GetAll()
        {
            List<ResourceBase> coreObjects = new List<ResourceBase>();

            ICollection<ResourceBase> inMemoryObject = InMemoryRepositorySingleton.Instance.GetAll(this);

            if (inMemoryObject != null)
            {
                coreObjects.AddRange(inMemoryObject.OfType<ResourceBase>());

                return (List<ResourceBase>)inMemoryObject;
            }

            return coreObjects;
        }


        public ResourceBase GetByID(Guid id)
        {
            return InMemoryRepositorySingleton.Instance.GetByID(id, this);
        }

        public void Save()
        {
            InMemoryRepositorySingleton.Instance.Save(this);
        }

        public void Delete()
        {
            InMemoryRepositorySingleton.Instance.Delete(this);
        }
    }
}
